/*****************************************************************************
 * Copyright (C) 2009 Gwathuirim
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Public License for more details
 *
 * To obtain a copy of the GNU General Public License, please see
 * <http://www.gnu.org.licenses/>
 *****************************************************************************/

// ==UserScript==
// @name           Imperion - EventNotifier
// @namespace      http://code.google.com/p/imperion-gm/
// @include        http://*.imperion.*/fleetBase/*
// @require        http://github.com/bcmpinc/travian-timeline-script/raw/I_0.07/jquery-latest.js
// @require        http://github.com/bcmpinc/travian-timeline-script/raw/I_0.07/patch.js
// @require        http://github.com/bcmpinc/travian-timeline-script/raw/I_0.07/date.js
// @require        http://github.com/bcmpinc/travian-timeline-script/raw/I_0.07/feature.js
// @require        http://imperion-gm.googlecode.com/svn/trunk/resources/settings.js
// @require        http://github.com/bcmpinc/travian-timeline-script/raw/I_0.07/events.js
// ==/UserScript==

(function() {

var SOUNDMANAGER_BASE_URL = "http://imperion-gm.googlecode.com/svn/trunk/resources/";

function preCreateSoundEffects() {
	soundManager.createSound('generalEvent', SOUNDMANAGER_BASE_URL + 'generalEvent.mp3');
	soundManager.play('generalEvent');
}

/**
 * Inserts raw JavaScript.
 */
function insertJavaScriptResource(javaScriptResource) {
	var scriptElement = document.createElement("script");
	scriptElement.setAttribute("type", "text/javascript");
	scriptElement.setAttribute("src", javaScriptResource);
	
	document.body.appendChild(scriptElement);
}

/**
 * Inserts raw JavaScript.
 */
function insertRawJavaScript(rawJavaScriptArray) {
	var scriptElement = document.createElement("script");
	scriptElement.setAttribute("type", "text/javascript");
	
	for (var i = 0; i < rawJavaScriptArray.length; i++) {
		scriptElement.innerHTML += rawJavaScriptArray[i] + "\n";
	}
	
	document.body.appendChild(scriptElement);
}

/**
 * Inserts custom JavaScript into the remote Page.
 */
function insertJavaScriptFunctions(functionArray) {
	var scriptElement = document.createElement("script");
	scriptElement.setAttribute("type", "text/javascript");
	
	for (var i = 0; i < functionArray.length; i++) {
		scriptElement.innerHTML += String(functionArray[i]) + "\n\n";
	}
	
	document.body.appendChild(scriptElement);
}

/* We require an external script injected into the remote site and some configuration */
insertJavaScriptResource(SOUNDMANAGER_BASE_URL + "soundmanager2.js");
insertJavaScriptFunctions(new Array(
	preCreateSoundEffects
));

insertRawJavaScript(new Array(
	"var SOUNDMANAGER_BASE_URL = '" + SOUNDMANAGER_BASE_URL + "';", //Adding the constant for the BASE URL
	"soundManager.flashVersion = 9;", //Set Flash version to 9
	"soundManager.debugMode = false;",
	"soundManager.url = '" + SOUNDMANAGER_BASE_URL + "soundmanager2_flash9_xdomain.swf';", //xdomain flash is required
	"soundManager.onload = preCreateSoundEffects;" //PreCreateSoundEffects onLoad
));

	Events.s.events.read();
    var events = Events.events;
    for (v in events) {
        try {
            var planet=Settings.planet_names[v];
            for (e in events[v]) {
                var evt = events[v][e];
				alert(evt);
            }
        } catch (e) {
			alert(e);
        }
    }

})();